<script lang="ts" setup xmlns="http://www.w3.org/1999/html">
import {onMounted, ref} from 'vue'
import * as Api from '@/api/swiper'
import {Plus, Upload} from '@element-plus/icons-vue'
import {Search} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {baseURL} from '@/utils/request'
const drawer = ref(false)
const tableData =ref([])
let total = ref(0)
let pageSize = ref(25)
let currentPage = ref(1)
let title = ref()
const getData =async ()=> {
  let result = await Api.page({
    "currentPage": currentPage.value,
    "pageSize": pageSize.value,
    "title": title.value
  })
  if (result.code === 200){
    tableData.value = result.data.rows
    total.value = result.data.total
  }
}
const handleCurrentChange = (e) => {
  currentPage.value = e
  getData()
}
const handleSizeChange = (e)=>{
  pageSize.value = e
  getData()
}
onMounted(async () => {
   await getData()
})
const shuaxin =async ()=> {
  title.value='';
  let result = await Api.page({
    "teachId":'',
  })
  if (result.code === 200){
    tableData.value = result.data.rows
  }
}
const role = JSON.parse(localStorage.getItem("name"))
//add
//抽屉
const form = ref({
  id :'',
  // survey: '', //调查问卷链接
  title: '', //标题
  img: '', //图片地址
  url: '', //链接地址
  createBy: role,
  updateBy: role

})

let isAdd = ref(true)
const btuAdd = ()=> {
  drawer.value=true
  // form.value.survey= ""
  form.value.title = ""
  form.value.img= ""
  form.value.url = ""
}
const add = async () => {
  let result = await Api.add({
    // "survey" : form.value.survey,
    "title": form.value.title,
    "img":form.value.img,
    "url":form.value.url,
    "createBy":form.value.createBy
  })
  if(result.code === 200){
    getData()
    drawer.value=false
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
}
//修改
const update = async (id) => {
  let result =await Api.update(id,{
    // "survey" : form.value.survey,
    "title": form.value.title,
    "img":form.value.img,
    "url":form.value.url,
    "createBy":form.value.createBy
  })

  if (result.code === 200){
    getData()
    drawer.value=false
    base64Image.value = ''
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
}
const btuUpdate = (data)=> {
  console.log(data)
  isAdd.value = false
  drawer.value=true
  form.value.id = data.id
  // form.value.survey = data.survey
  form.value.title = data.title
  form.value.img = data.img
  form.value.url = data.url
}
const submit = ()=> {
  if (isAdd.value) {
    add()
  } else {
    update(form.value.id)
  }
}
const dele = async (id) => {
    console.log(id);
    await ElMessageBox.confirm('您确定要删除吗', '温馨提示', {
      type: 'warning',
      confirmButtonText: '确认',
      cancelButtonText: '取消',
    }).then(async () => {
      let result = await Api.dele(id)
      if (result.code === 200) {
        getData()
        ElMessage.success(result.message)
      } else {
        ElMessage.error(result.message)
      }
    })
  }
//图片
const base64Image = ref('');
const handleFileChange = (event) => {
  const file = event.target.files[0];

  if (!file) {
    alert('请选择图片');
    return;
  }

  const reader = new FileReader();

  reader.onload = (e) => {
    base64Image.value = e.target.result;
    console.log('Base64 Encoded Image:', base64Image.value);
    form.value.img = base64Image.value
  };

  reader.onerror = (error) => {
    console.error('Error: ', error);
  };

  reader.readAsDataURL(file);
};
</script>
<template>
    <div style="position: absolute;top: 100px;width: 90%;height: 40px;border-bottom: solid 1px rgb(221.7, 222.6, 224.4);margin: auto">
      <el-row>
        <el-col :span="8">
          <el-button @click="btuAdd" style="border: solid 1px #4285F4;border-radius: 20px;width: 150px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);">
            上传信息<el-icon class="el-icon--right"><Upload /></el-icon>
          </el-button>

          <div class="grid-content ep-bg-purple" />
        </el-col>
        <el-col :span="8">
          <div class="grid-content ep-bg-purple-light" />
        </el-col>
        <el-col :span="8">
          <el-input
              v-model="title"
              style="width: 240px;padding-left: 5px;"
              placeholder="请输入标题"
              clearable
          />
          <el-button style="border: solid 1px #4285F4;margin-left: 5px;border-radius: 20px;width: 20px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);">
            <el-icon @click="getData" class="el-icon--right"><el-icon><Search /></el-icon></el-icon>
          </el-button>
          <el-button style="border: solid 1px #4285F4;border-radius: 20px;width: 20px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);" @click="shuaxin">
            <el-icon ><el-icon><RefreshRight /></el-icon></el-icon>
          </el-button>
        </el-col>
      </el-row>
    </div>
  <div style="height: 88%;width: 100%;margin-top: 80px">
    <el-table :data="tableData"
              :default-sort="{ prop: 'date', order: 'descending' }"
              style="width: 100%;height: 95%">
      <el-table-column fixed type="index" label="编号" width="80" />
      <el-table-column fixed prop="id" label="id" width="100" />
      <el-table-column fixed prop="title" label="标题" width="150" />
      <el-table-column  prop="img" label="图片地址" width="250" >
        <template #default="scope">
          <img :src="scope.row.img" alt="Avatar" style="width: 100%; height: auto;">
        </template>
      </el-table-column>
      <el-table-column prop="url" label="链接地址" width="250" />
      <el-table-column prop="createBy" label="编辑人" width="150" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="dele(scope.row.id)"
          >
            删除
          </el-button>
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="btuUpdate(scope.row)"
          >
            修改
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[25, 50, 100, 200]"

        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 10px"
    />
  </div>
    <!--抽屉-->
    <el-drawer
        v-model="drawer"
        :title="isAdd ? '新增:' : '修改:'"
        size="40%"
    >
      <div style="width: 100% ; border-top: #6b778c 1px solid;margin-bottom: 50px"></div>
      <span>
        <el-form :model="form" label-width="auto" style="max-width: 600px;font-size: 20px">
    <el-form-item v-show="!isAdd" label="id:">
      <el-input v-model="form.id"  style="width: 400px" />
    </el-form-item>
          <el-form-item label="标题:">
      <el-input v-model="form.title" style="width: 400px" />
    </el-form-item>
    <el-form-item label="地址链接:">
      <el-input v-model="form.url" style="width: 400px"/>
    </el-form-item>
          <el-form-item v-show="!isAdd" label="修改人:">
      <el-input  disabled  v-model="form.createBy" style="width: 400px"/>
    </el-form-item>
     <el-form-item v-show="isAdd" label="创造人:">
      <el-input disabled v-model="form.createBy" style="width: 400px"/>
    </el-form-item>
              <el-form-item label="图片上传:"  style="display: flex;justify-content: center">
      <div class="file-upload">
  <label for="file-input" class="custom-file-upload">
    选择图片
    <input type="file" id="file-input" @change="handleFileChange" accept="image/*" style="display: none;">
  </label>
</div>
    <el-button @click="submit()" style="border: solid 1px #4285F4;border-radius: 20px;height:42px;width:200px;color: #4285F4;margin-top:15px;margin-left: 50px;">发布</el-button>
    </el-form-item>
     <el-form-item >
       <div v-if="base64Image" style="margin-top: 20px;margin-left: 80px">
<!--    <h3>图片:</h3>-->
    <img :src="base64Image" style="max-width: 400px;max-height: 350px; border: 1px solid #ddd; border-radius: 4px; padding: 5px;">
  </div>
    </el-form-item>
  </el-form>
      </span>
    </el-drawer>
</template>
<style scoped>
.avatar-uploader .avatar {
  width: 320px;
  height: 238px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.file-upload {
 height: 25px !important;
}

.custom-file-upload {
  padding-bottom: 40px;
  text-align: center;
  height: 25px;
  width: 150px;
  display: inline-block;
  border: solid 1px #4285F4;
  border-radius: 20px;
  color: #4285F4;
  background-color:rgb(216.8, 235.6, 255);
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.custom-file-upload:hover {
  background-color: white;
}
.el-drawer__title{
  font-size: 35px;
  font-weight:normal;
  color: black;
}
</style>